package com.xyht.medicine_management_system.modules.user.controller;


import com.xyht.medicine_management_system.auth.util.JwtTokenUtil;
import com.xyht.medicine_management_system.common.annotation.RequestPass;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.medicine_management_system.common.util.CacheUtil;
import com.xyht.medicine_management_system.modules.user.entity.ChangeUserPassword;
import com.xyht.medicine_management_system.modules.user.entity.CodeEntity;
import com.xyht.medicine_management_system.modules.user.entity.VerifyCode;
import com.xyht.medicine_management_system.modules.user.entity.req.*;
import com.xyht.medicine_management_system.modules.user.service.MmsUserService;
import com.xyht.medicine_management_system.modules.user.util.verify_code.VerifyCodeUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-11-12
 */
@RestController
@SecretBody
@RequestMapping("/user")
public class MmsUserController {
    @Resource
    private MmsUserService mmsUserService;
    @Resource
    private CacheUtil cacheUtil;
    @Resource
    private VerifyCodeUtil verifyCodeUtil;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @PostMapping("/login")
    @ApiModelProperty(value = "用户密码登录")
    @RequestPass
    public ResponseResult webLogin(@RequestBody SmsUserLoginReq smsUserLoginReq) {
        return mmsUserService.webLogin(smsUserLoginReq);
    }

    @PostMapping("/phoneLogin")
    @ApiModelProperty(value = "用户手机验证码登录")
    @RequestPass
    public ResponseResult phoneLogin(@RequestBody SmsUserLoginReq smsUserLoginReq) {
        return mmsUserService.phoneLogin(smsUserLoginReq);
    }

    @GetMapping("/phoneVerifyCode")
    @ApiModelProperty(value = "发送手机验证码")
    @RequestPass
    public ResponseResult phoneVerifyCode(@RequestParam("phoneNum") String phoneNum) {
        return mmsUserService.phoneVerifyCode(phoneNum);
    }

    @PostMapping("/findPassword")
    @ApiModelProperty(value = "手机验证码密码找回")
    @RequestPass
    public ResponseResult findPassword(@RequestBody FindPasswordReq findPasswordReq) {
        return mmsUserService.findPassword(findPasswordReq);
    }

    @PostMapping("/userCheck")
    @ApiModelProperty(value = "校验用户是否存在")
    @RequestPass
    public ResponseResult checkUserExist(@RequestBody SmsUserLoginReq smsUserLoginReq) {
        return mmsUserService.checkUserExist(smsUserLoginReq);
    }

    @PostMapping("/oldFind")
    @ApiModelProperty(value = "原密码找回密码")
    public ResponseResult userChangePasswordByOldPassword(@RequestBody ChangeUserPassword changeUserPassword, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return mmsUserService.userChangePasswordByOldPassword(user_id, changeUserPassword);
    }

    @PostMapping("/register")
    @ApiModelProperty(value = "用户账号注册")
    @RequestPass
    public ResponseResult userRegister(@RequestBody SmsUserRegisterReq registerReq) {
        return mmsUserService.userRegister(registerReq);
    }

    @PostMapping("/phoneRegister")
    @ApiModelProperty(value = "手机快捷注册")
    @RequestPass
    public ResponseResult userPhoneRegister(@RequestBody SmsUserRegisterReq registerReq) {
        return mmsUserService.userPhoneRegister(registerReq);
    }

    @ApiModelProperty("获取验证码")
    @GetMapping("/verifyCode")
    @RequestPass
    public ResponseResult verifyCode() {
        Map<String, Object> map = new HashMap<>();
        CodeEntity codeEntity = new CodeEntity();

        try {
            //设置长宽
            VerifyCode verifyCode = verifyCodeUtil.generateImg(80, 28);
            String code = verifyCode.getCode();
            String base64Code = Base64.getEncoder().encodeToString(verifyCode.getImgBytes());
            String uid = String.valueOf(new Date().getTime());
            codeEntity.setCode(code);
            codeEntity.setUid(uid);
            codeEntity.setCreate(new Date());


            //存验证码
            cacheUtil.add(uid, code, 1, TimeUnit.MINUTES);

            map.put("img", base64Code);
            map.put("uid", uid);
            map.put("verifyCode", code);

        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return CommonResult.success(map);
    }

    @GetMapping("/list")
    @ApiModelProperty(value = "用户列表")
    public ResponseResult getUserList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "userType",required = false) Integer userType,
                                      @RequestParam(value = "searchStr",required = false) String searchStr) {
        return mmsUserService.getUserList(userType, searchStr, pageNum, pageSize);
    }

    @GetMapping("/detail")
    @ApiModelProperty(value = "用户详情")
    public ResponseResult getUserDetail(@RequestParam("userId") String userId) {
        return mmsUserService.getUserDetail(userId);
    }

    @PostMapping("/update")
    @ApiModelProperty(value = "后台修改用户信息")
    public ResponseResult updateUser(@RequestBody MmsUserReq mmsUserReq) {
        return mmsUserService.updateUser(mmsUserReq);
    }
    @PostMapping("/add")
    @ApiModelProperty(value = "后台添加用户信息")
    public ResponseResult addUser(@RequestBody MmsUserReq mmsUserReq) {
        return mmsUserService.addUser(mmsUserReq);
    }

    @PostMapping("/updateProfile")
    @ApiModelProperty(value = "用户修改用户头像")
    public ResponseResult updateUserProfile(@RequestBody MmsUserReq mmsUserReq,HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return mmsUserService.updateUserProfile(mmsUserReq,user_id);
    }

    @PostMapping("/updateNickName")
    @ApiModelProperty(value = "用户修改用户昵称")
    public ResponseResult updateUserNickName(@RequestBody MmsUserReq mmsUserReq,HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return mmsUserService.updateUserNickName(mmsUserReq,user_id);
    }
    @PostMapping("/updateUserRole")
    @ApiModelProperty(value = "修改用户关联角色")
    public ResponseResult updateUserRole(@RequestBody UserRoleReq userRoleReq) {
        return mmsUserService.updateUserRole(userRoleReq);
    }

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
    public ResponseResult getUserInfo(HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsUserService.getUserInfo(userId);
    }

    @PostMapping("/invitationCode")
    @ApiModelProperty(value = "生成用户邀请码")
    public ResponseResult generateSelfInvitationCode(HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsUserService.generateSelfInvitationCode(userId);
    }
}

